package com.zhao.controller.show;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.Util.FileUtil;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.*;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/fileDown")
public class FileDownController {
    @Autowired
    DatafileService datafileService;
    @Autowired
    WalletService walletService;
    @Autowired
    UserService userService;
    @Autowired
    TransactionService transactionService;
    @Autowired
    FileStatisticService fileStatisticService;
    @ResponseBody
    @RequestMapping("/down/{id}")
    public void down(@PathVariable Integer id, HttpServletResponse res) throws UnsupportedEncodingException {
        Datafile datafile = datafileService.getById(id);
        String title=datafile.getTitle()+"."+datafile.getType();
        FileUtil.down(res,datafile.getAbsolutepath(),title);
    }
    @ResponseBody
    @RequestMapping("/check/{id}")
    @Transactional
    public String check(@PathVariable Integer id){
        //获取当前用户
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        //获取当前用户钱包
        Wallet wallet = walletService.getOne(new QueryWrapper<Wallet>().eq("uid", loginUser.getUserId()));
        //获取商品文件信息
        Datafile datafile = datafileService.getById(id);
        //获取卖家钱包
        User user_account = userService.getOne(new QueryWrapper<User>().eq("user_account", datafile.getAuthor()));
        Wallet wallet1 = walletService.getOne(new QueryWrapper<Wallet>().eq("uid", user_account.getUserId()));

        int walletGods=Integer.parseInt(wallet.getGolds());//买
        int datafileGods=Integer.parseInt(datafile.getPrice());//卖
        System.out.println(walletGods+"--"+datafileGods);
        if (walletGods<datafileGods){
            return "-1";//积分不足请，先充值！
        }
        //买家剩余
        int balance=walletGods-datafileGods;
        //卖收益后
        int hou=Integer.parseInt(wallet1.getGolds())+Integer.parseInt(datafile.getPrice());
        wallet.setGolds(balance+"");
        wallet1.setGolds(hou+"");
        //修改买家钱包
        walletService.updateById(wallet);
        walletService.updateById(wallet1);
        //生成消费记录
        Transaction transaction = new Transaction();
        transaction.setBuyers(loginUser.getUserId());
        transaction.setSeller(user_account.getUserId());
        transaction.setFileId(datafile.getFileId());
        transaction.setType("资源文件下载");
        transactionService.save(transaction);

        Filestatistic file_id = fileStatisticService.getOne(new QueryWrapper<Filestatistic>().eq("file_id", datafile.getFileId()));
        //设置 下载量+1
        file_id.setDownup(file_id.getDownup()+1);
        //设置总收入金币
        file_id.setSumprice((Integer.parseInt(file_id.getSumprice())+Integer.parseInt(datafile.getPrice()))+"");
        fileStatisticService.updateById(file_id);
        return "1";
    }
}
